﻿@page "/users/{id}/{action}"
@using SimpleIdServer.IdServer.Website.Resources;
@using SimpleIdServer.IdServer.Website.Stores.RealmStore;
@using SimpleIdServer.IdServer.Website.Stores.UserStore;
@inherits Fluxor.Blazor.Web.Components.FluxorLayout
@inject NotificationService notificationService
@inject NavigationManager navigationManager
@layout MainLayout
@inject IState<UserState> userState
@inject IDispatcher dispatcher

<RadzenBreadCrumb>
    <RadzenBreadCrumbItem Path="/users" Text="@Global.Users" />
    <RadzenBreadCrumbItem Text="@Global.UserDetails" />
</RadzenBreadCrumb>

<RadzenText class="mt-3 mb-3 no-margin" Text="@userState.Value.User?.Name" TextStyle="TextStyle.DisplayH3" />

@if (!string.IsNullOrWhiteSpace(userState.Value.User?.Source))
{
    <RadzenBadge class="mb-3" Text="@userState.Value.User?.Source" IsPill="true" BadgeStyle="BadgeStyle.Info"></RadzenBadge>
}

@if(userState.Value.User != null)
{
    <RadzenBadge class="mb-3" IsPill=true Text="@(userState.Value.User.Status == UserStatus.ACTIVATED ? Global.Active : Global.Blocked)" BadgeStyle="@(userState.Value.User.Status == UserStatus.ACTIVATED ? BadgeStyle.Success : BadgeStyle.Danger)"></RadzenBadge>
}

<RadzenCardLoader Variant="Variant.Filled" IsLoading=@userState.Value.IsLoading>
    <RadzenTabs Change=@OnChange @bind-SelectedIndex="selectedIndex">
        <Tabs>
            <RadzenTabsItem Text="@Global.Details">
                <UserSettings User="@userState.Value.User"></UserSettings>
            </RadzenTabsItem>
            <RadzenTabsItem Text="@Global.Consents" >
                <UserConsents User="@userState.Value.User"></UserConsents>
            </RadzenTabsItem>
            <RadzenTabsItem Text="@Global.Credentials">
                <UserCredentials User="@userState.Value.User"></UserCredentials>
            </RadzenTabsItem>
            <RadzenTabsItem Text="@Global.Groups">
                <UserGroups User="@userState.Value.User"></UserGroups>
            </RadzenTabsItem>
            <RadzenTabsItem Text="@Global.Claims">
                <UserClaims UserId="@userState.Value.User?.Id"></UserClaims>
            </RadzenTabsItem>
            <RadzenTabsItem Text="@Global.Sessions">
                <UserSessions User="@userState.Value.User"></UserSessions>
            </RadzenTabsItem>
            <RadzenTabsItem Text="@Global.ExternalIdProviders">
                <UserProfiles User="@userState.Value.User"></UserProfiles>
            </RadzenTabsItem>
        </Tabs>
    </RadzenTabs>
</RadzenCardLoader>

@code {
    Dictionary<string, int> mappingActionNameToIndex = new Dictionary<string, int>
    {
        { "settings", 0 },
        { "consents", 1 },
        { "credentials", 2 },
        { "groups", 3 },
        { "claims", 4 },
        { "sessions", 5 },
        { "profiles", 6 }
    };
    [Parameter]
    public string id { get; set; } = null!;

    [Parameter]
    public string? action { get; set; } = null;

    int selectedIndex = 0;

    protected override void OnAfterRender(bool firstRender)
    {
        base.OnAfterRender(firstRender);
        if(firstRender)
        {
            SubscribeToAction<GetUserFailureAction>((act) =>
            {
                notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Error, Summary = act.ErrorMessage });
                StateHasChanged();
            });
            SubscribeToAction<SelectRealmSuccessAction>((act) =>
            {
                navigationManager.NavigateTo("/users");
            });
        }
    }

    protected override void OnParametersSet()
    {
        base.OnParametersSet();
        if (string.IsNullOrWhiteSpace(id)) return;
        if (!string.IsNullOrWhiteSpace(action) && mappingActionNameToIndex.ContainsKey(action))
            selectedIndex = mappingActionNameToIndex[action];
        dispatcher.Dispatch(new GetUserAction { UserId = id });
    }

    void OnChange(int index)
    {
        var rec = mappingActionNameToIndex.Single(kvp => kvp.Value == index);
        navigationManager.NavigateTo($"/users/{id}/{rec.Key}");
    }
}